/**
 * 
 */
package com.newtribe.access.context;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.net.SocketException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.newtribe.access.AppEngine;
import com.newtribe.access.StreamUtil;
import com.newtribe.tk.TKWorker;
import com.newtribe.tk.Task;
import com.newtribe.tk.ThreadPool;

/**
 * @author newtribe
 *
 */
public class PeerContext {
	
	
	 public static Log log =LogFactory.getLog(PeerContext.class) ;

	private volatile  Socket rdpRequestSocket ;
	private volatile Socket rdpResponseSocket ;
	private int status =0;
	private String GUID ;//A REQUEST AND RESPONSE SHARE GLOBAL IDENTIFIER
    boolean stop =false  ;

	private ThreadPool pool =null ;
	
	public PeerContext(String guid){
		this.GUID=guid;
	}
	
	public Socket getRdpRequestSocket() {
		return  this.rdpRequestSocket;
	}



	public void setRdpRequestSocket(Socket rdpRequestSocket) {
		status =status |1 ;
		this.rdpRequestSocket = rdpRequestSocket;
	}



	public Socket getRdpResponseSocket() {
		return this.rdpResponseSocket;
	}



	public void setRdpResponseSocket(Socket rdpResponseSocket) {
		status =status |2 ;

		this.rdpResponseSocket = rdpResponseSocket;
	}



	public String getGUID() {
		return GUID;
	}



	public void setGUID(String guid) {
		GUID = guid;
	}



	public int getStatus() {
		return status;
	}
	
	public void stop(){
		this.stop =true ;
	}
	public void start() throws Exception{
		if (this.getStatus()!=3)
			return ;
		this.stop =false;
		ThreadPool pool =AppEngine.getEngine().getExecuteQueue();
		TKWorker worker = pool.findTKWorker();
		RequestTask resTask =new RequestTask();
		worker.dispatch(resTask) ;
		
		
		TKWorker worker2 = pool.findTKWorker();
		ResponseTask responseTask =new ResponseTask();
		worker2.dispatch(responseTask) ;
	}
	public void recycleResource()throws Exception {
		
		if (this.rdpRequestSocket !=null)
		this.rdpRequestSocket.close();
		this.rdpRequestSocket=null ;
		
		if (this.rdpResponseSocket !=null)
		this.rdpResponseSocket.close();
		this.rdpResponseSocket =null;
		
	}
	
	 class RequestTask extends Task {
		
	public boolean execute () throws Exception {
	
		
//		InputStream in  =rdpRequestSocket.getInputStream();
//		OutputStream out =rdpRequestSocket.getOutputStream();
//		
//		InputStream rdpIn =rdpResponseSocket.getInputStream();
//		OutputStream rdpOut =rdpResponseSocket.getOutputStream();
	    DataInputStream in =new DataInputStream(getRdpRequestSocket().getInputStream()) ;
	    DataOutputStream out  =new DataOutputStream(getRdpRequestSocket().getOutputStream()) ;
	    
	    DataInputStream rdpIn =new DataInputStream(getRdpResponseSocket().getInputStream()) ;
	    DataOutputStream rdpOut  =new DataOutputStream(getRdpResponseSocket().getOutputStream() );
	    
	    
	    while (!stop) {
			try {
				//System.out.println("======================================send ");
				StreamUtil.fp(in,rdpOut);
//				System.out.println("======================================recieve ");
//
//				StreamUtil.fp(rdpIn,out);

				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				break ;
			}
	    }

	    recycleResource(); 
			return true;
	}
	}
	 class ResponseTask extends Task {
		
		public boolean execute () throws Exception {
			
//			
		
		    DataInputStream in =new DataInputStream(getRdpRequestSocket().getInputStream()) ;
		    DataOutputStream out  =new DataOutputStream(getRdpRequestSocket().getOutputStream()) ;
		    
		    DataInputStream rdpIn =new DataInputStream(getRdpResponseSocket().getInputStream()) ;
		    DataOutputStream rdpOut  =new DataOutputStream(getRdpResponseSocket().getOutputStream() );
		    
		    while (!stop) {
				try {
//					System.out.println("======================================send ");
//					StreamUtil.fp(in,rdpOut);
					//System.out.println("======================================recieve ");

					StreamUtil.fp(rdpIn,out);
					
				} catch (SocketException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					stop=true;
					break ;
				}
		    }
		    recycleResource(); 

		  
			return true;
		}
		
	}
		
}
